package com.xianniu.ltc.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReUtil;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.xianniu.ltc.model.CustomerCellStyle;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author: dingyx
 * @Date: 2021/11/5 2:43 下午
 */
@Data
@Slf4j
public class CustomCellWriteHandler implements CellWriteHandler {

    private List<CustomerCellStyle> cellStyles;

    public CustomCellWriteHandler(){
    }
    public CustomCellWriteHandler(List<CustomerCellStyle> cellStyles){
        this.cellStyles = cellStyles;
    }



    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                 Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
                                Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 不对头进行任何操作
        if(isHead){
            return;
        }
        // 查询是否有需要特殊处理的cell
        Optional<CustomerCellStyle> optional = cellStyles.stream().
                filter(b -> b.getColumnIndex() == cell.getColumnIndex()).
                filter(b -> b.getRowIndex() == cell.getRowIndex()).
                findAny();
        short fillForegroundColor = IndexedColors.AUTOMATIC.index;
        if(optional.isPresent()) {
            fillForegroundColor = optional.get().getIndexColor();
        }
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 9);
        font.setFontName("微软雅黑");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(fillForegroundColor);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setFont(font);
        // 数字类型需要右对齐
        if (CellType.NUMERIC.equals(cell.getCellType())) {
            cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        }
        // 文本类型需要特殊处理的情况
        if (CellType.STRING.equals(cell.getCellType())){
            if(ReUtil.isMatch("([0-9]+,?)+\\.[0-9]{2}", cell.getStringCellValue())) {
                // 千分位字符类型，需要右对齐
                cellStyle.setAlignment(HorizontalAlignment.RIGHT);
            }else if(cell.getStringCellValue().contains("%")){
                // 带%号，需要居中
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            }
        }
        cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        cell.setCellStyle(cellStyle);
    }
}